package com.weirdor.wxchat.facade.user;

import com.weirdor.wxchat.facade.user.vo.WxTag;
import com.weirdor.wxchat.facade.user.vo.WxTagUser;
import com.weirdor.wxchat.facade.user.vo.WxTagUserList;
import com.weirdor.wxchat.support.exception.OtherAPIException;
import com.weirdor.wxchat.support.exception.WexinReqException;
import com.weirdor.wxchat.support.httpClient.HttpsClient;
import com.xiaoleilu.hutool.http.HttpUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.json.HTTP;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by weirdor on 2017/11/28.
 */
public class TagAPI {


    private static Logger logger = LoggerFactory.getLogger(TagAPI.class);

    //创建标签
    private static String create_tag = "https://api.weixin.qq.com/cgi-bin/tags/create?access_token=ACCESS_TOKEN";

    //获取创建的标签
    private static String get_tag = "https://api.weixin.qq.com/cgi-bin/tags/get?access_token=ACCESS_TOKEN";

    //编辑标签
    private static String update_tag = "https://api.weixin.qq.com/cgi-bin/tags/update?access_token=ACCESS_TOKEN";

    //删除标签
    private static String delete_tag = "https://api.weixin.qq.com/cgi-bin/tags/delete?access_token=ACCESS_TOKEN";

    //获取标签下粉丝列表
    private static String get_tag_user = "https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token=ACCESS_TOKEN";

    //批量为用户打标签
    private static String batchtagging = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=ACCESS_TOKEN";

    //批量为用户取消标签
    private static String batchuntagging = "https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging?access_token=ACCESS_TOKEN";

    //获取用户身上的标签列表
    private static String getidlist= "https://api.weixin.qq.com/cgi-bin/tags/getidlist?access_token=ACCESS_TOKEN";


    /**
     * 创建标签
     * @param accessToken
     * @param tagName
     * @return
     * @throws WexinReqException
     */
    public static JSONObject createTag(String accessToken, String tagName) throws WexinReqException, OtherAPIException {
        JSONObject result = null;
        if (accessToken != null) {
            String requestUrl = create_tag.replace("ACCESS_TOKEN", accessToken);
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("name", tagName);
            JSONObject obj = new JSONObject();
            obj.put("tag", map);
            logger.info("创建标签方法执行前json参数---obj: "+obj.toString());
            String response= HttpsClient.doPost(requestUrl,obj.toString());
            result = JSONObject.fromObject(response);
            logger.info("创建标签方法执行后json参数 : "+result.toString());
        }
        return result;
    }

   /**
    * 获取公众号已创建的标签
    * */
    /**
     * 获取标签
     */
    public static List<WxTag> getTags(String accessToken){
        List<WxTag> list = null;
        if(accessToken != null){
            String requestUrl = get_tag.replace("ACCESS_TOKEN", accessToken);
            //logger.info("创建标签方法执行前json参数 obj: "+obj.toString());
            String resopnse= HttpUtil.get(requestUrl);
            JSONObject result = JSONObject.fromObject(resopnse);
            Object error = result.get("errmsg");
            if(error == null){
                String tags = result.getString("tags");
                JSONArray jsonArray = JSONArray.fromObject(tags);
                list = JSONArray.toList(jsonArray, WxTag.class);
                return list;
            }
            logger.info("获取标签方法执行后json参数 : "+result.toString());
        }
        return list;
    }

    /**
     * 编辑标签
     */
    public static JSONObject updateTag(String accessToken,int id,String name) throws OtherAPIException {
        JSONObject result = null;
        if(accessToken != null){
            String requestUrl = update_tag.replace("ACCESS_TOKEN", accessToken);
            Map<String,Object> data = new HashMap<String,Object>();
            data.put("id", id);
            data.put("name", name);
            JSONObject obj = new JSONObject();
            obj.put("tag", data);
            logger.info("编辑标签方法执行前json参数---obj: "+obj.toString());
            String response= HttpsClient.doPost(requestUrl,obj.toString());
            result = JSONObject.fromObject(response);
            logger.info("编辑标签方法执行后json参数 : "+result.toString());
        }
        return result;
    }

    /**
     * 删除标签
     */
    public static JSONObject delete(String accessToken,String id) throws OtherAPIException {
        JSONObject result = null;
        if(accessToken != null){
            String requestUrl = delete_tag.replace("ACCESS_TOKEN", accessToken);
            Map<String,Object> data = new HashMap<String,Object>();
            data.put("id", id);
            JSONObject obj = new JSONObject();
            obj.put("tag", data);
            logger.info("删除标签方法执行前json参数---obj: "+obj.toString());
            String response= HttpsClient.doPost(requestUrl,obj.toString());
            result = JSONObject.fromObject(response);
            logger.info("删除标签方法执行后json参数 : "+result.toString());
        }
        return result;
    }

    /**
     * 获取标签下粉丝列表
     */
    public static WxTagUser getTagUser(String accessToken,int tagid,String next_openid){
        WxTagUser tagUser = null;
        WxTagUserList users = null;
        try {
            if(accessToken != null){
                String requestUrl = get_tag_user.replace("ACCESS_TOKEN", accessToken);
                Map<String,Object> data = new HashMap<String,Object>();
                data.put("tagid", tagid);
                data.put("next_openid", next_openid);
                JSONObject obj = JSONObject.fromObject(data);
                logger.info("获取标签下粉丝列表方法执行前json参数---obj: "+obj.toString());
                String response= HttpUtil.post(requestUrl,data);
                JSONObject result = JSONObject.fromObject(response);
                System.out.println(result);
                Integer error = (Integer) result.get("errmsg");
                if(error == null){
//					Integer count = (Integer) obj.get("count");
//					String nextOpenid = obj.getString("next_openid");
//					Object dataStr = obj.get("data");
//					JSONObject dataJson = JSONObject.fromObject(dataStr);
//					JSONArray jsonArray = dataJson.getJSONArray("openid");
//					List<String> list = JSONArray.toList(jsonArray, String.class);
//					users.setOpenid(list);
//					tagUser.setCount(count);
//					tagUser.setData(users);
//					tagUser.setNext_openid(nextOpenid);
                    tagUser  = (WxTagUser) JSONObject.toBean(result, WxTagUser.class);
                }
                logger.info("获取标签下粉丝列表方法执行后json参数 : "+result.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tagUser;
    }


    /**
     * 批量为用户取消标签
     */
    public static JSONObject batchuntagging(String accessToken,List<String> openid_list,int tagid){
        JSONObject result = null;
        if(accessToken != null){
            String requestUrl = batchuntagging.replace("ACCESS_TOKEN", accessToken);
            Map<String,Object> data = new HashMap<String,Object>();
            data.put("openid_list", openid_list);
            data.put("tagid", tagid);
            JSONObject obj = JSONObject.fromObject(data);
            logger.info("批量为用户取消标签 方法执行前json参数---obj: "+obj.toString());
            String response= HttpUtil.post(requestUrl,data);
            result = JSONObject.fromObject(response);
            logger.info("批量为用户取消标签 方法执行后json参数 : "+result.toString());
        }
        return result;
    }

    /**
     * 批量为用户打标签
     */
    public static JSONObject batchtagging(String accessToken,List<String> openid_list,int tagid) throws OtherAPIException {
        JSONObject result = null;
        if(accessToken != null){
            String requestUrl = batchtagging.replace("ACCESS_TOKEN", accessToken);
            Map<String,Object> data = new HashMap<String,Object>();
            data.put("openid_list", openid_list);
            data.put("tagid", tagid);
            JSONObject obj = JSONObject.fromObject(data);
            logger.info("批量为用户打标签 方法执行前json参数---obj: "+obj.toString());
            String response= HttpsClient.doPost(requestUrl,obj.toString());
            result = JSONObject.fromObject(response);
            logger.info("批量为用户打标签 方法执行后json参数 : "+result.toString());
        }
        return result;
    }

    /**
     * 获取用户身上的标签列表
     */
    public static List<Integer> getidlist(String accessToken,String openid) throws OtherAPIException {
        List<Integer> list = null;
        if(accessToken != null){
            String requestUrl = getidlist.replace("ACCESS_TOKEN", accessToken);
            Map<String,Object> data = new HashMap<String,Object>();
            data.put("openid", openid);
            JSONObject obj = JSONObject.fromObject(data);
            logger.info("获取用户身上的标签列表 方法执行前json参数---obj: "+obj.toString());
            String response= HttpsClient.doPost(requestUrl,obj.toString());
            JSONObject result = JSONObject.fromObject(response);
            System.out.println(result);
            Object error = result.get("errmsg");
            if(error == null){
                JSONArray jsonArray = result.getJSONArray("tagid_list");
                list = JSONArray.toList(jsonArray, Integer.class);
            }
            logger.info("获取用户身上的标签列表 方法执行后json参数 : "+result.toString());
        }
        return list;
    }


    public static void main(String[] args) throws OtherAPIException, WexinReqException {
        List<String> list=new ArrayList<>();
        list.add("oTy4owQAS5l80L_y_VW_6wSRAtWM");
        System.out.println(getidlist("DHmd3qFzxjTALsqmTVTNOU4qmbUXqxiBlPEaGlts5SKaS1Nt0oHWQt8y7mb7JqLCtwxMAE_Q883YlkR8S12JiGYobNyFE-rdVTQSdXjEo0YKSCiAIABJT","oTy4owQAS5l80L_y_VW_6wSRAtWM"));
    }
}
